package com.zzyl.nursing;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

/**
 * 身份证工具类
 *
 * @author lingma
 */
public class IdCardUtils {

    /**
     * 根据身份证号码计算年龄
     *
     * @param idCard 身份证号码
     * @return 年龄
     * @throws IllegalArgumentException 身份证号码格式不正确时抛出异常
     */
/**
 * 从身份证号码中提取出生日期
 *
 * @param idCard 身份证号码
 * @return 出生日期 (LocalDateTime格式)
 */
public static LocalDateTime getBirthDateByIdCard(String idCard) {
    if (idCard == null || idCard.trim().isEmpty()) {
        return null;
    }

    try {
        // 18位身份证
        if (idCard.length() == 18) {
            String birthDateStr = idCard.substring(6, 14);
            String formattedDate = birthDateStr.substring(0, 4) + "-" + birthDateStr.substring(4, 6) + "-" + birthDateStr.substring(6, 8);
            return LocalDate.parse(formattedDate).atStartOfDay();
        }
        // 15位身份证
        else if (idCard.length() == 15) {
            String birthDateStr = idCard.substring(6, 12);
            String year = "19" + birthDateStr.substring(0, 2);
            String formattedDate = year + "-" + birthDateStr.substring(2, 4) + "-" + birthDateStr.substring(4, 6);
            return LocalDate.parse(formattedDate).atStartOfDay();
        }
    } catch (Exception e) {
        return null;
    }

    return null;
}

public static int getAgeByIdCard(String idCard) {
    if (idCard == null || idCard.trim().isEmpty()) {
        throw new IllegalArgumentException("身份证号码不能为空");
    }

    LocalDateTime birthDateTime = getBirthDateByIdCard(idCard);
    if (birthDateTime == null) {
        throw new IllegalArgumentException("身份证号码格式不正确");
    }

    try {
        LocalDate birthDate = birthDateTime.toLocalDate();
        LocalDate currentDate = LocalDate.now();
        return java.time.Period.between(birthDate, currentDate).getYears();
    } catch (Exception e) {
        throw new IllegalArgumentException("身份证号码中的出生日期格式不正确", e);
    }
}

    /**
     * 获取性别 (0-女, 1-男)
     *
     * @param idCard 身份证号码
     * @return 性别代码
     */
    public static Integer getGenderByIdCard(String idCard) {
        if (idCard == null || idCard.trim().isEmpty()) {
            return null;
        }

        String genderCode;
        if (idCard.length() == 18) {
            genderCode = idCard.substring(16, 17);
        } else if (idCard.length() == 15) {
            genderCode = idCard.substring(14, 15);
        } else {
            return null;
        }

        try {
            return Integer.parseInt(genderCode) % 2;
        } catch (NumberFormatException e) {
            return null;
        }
    }

    /**
     * 验证身份证号码格式是否正确
     *
     * @param idCard 身份证号码
     * @return 是否有效
     */
    public static boolean isValidIdCard(String idCard) {
        if (idCard == null) {
            return false;
        }

        // 长度验证
        if (idCard.length() != 15 && idCard.length() != 18) {
            return false;
        }

        // 15位身份证验证（全数字）
        if (idCard.length() == 15) {
            return idCard.matches("[0-9]{15}");
        }

        // 18位身份证验证（前17位为数字，最后一位为数字或X）
        if (idCard.length() == 18) {
            return idCard.matches("[0-9]{17}[0-9Xx]");
        }

        return false;
    }
}
